<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加载ArcGIS地图瓦片</title>
    <link rel="stylesheet" href="css/ol.css">
    <script src="js/ol.js"></script>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">

    // 设置地图中心，此处进行坐标转换， 把EPSG:4326(经纬度坐标)，转换为EPSG:3857(WGS84网络墨卡托(辅助球))坐标，因为ol默认使用的是EPSG:3857坐标
    let centerPos = ol.proj.transform([117.27, 31.68], 'EPSG:4326', 'EPSG:3857');

    //创建地图
    let map = new ol.Map({
        view: new ol.View({
            center: centerPos,//地图中心位置
            zoom: 10,//地图初始层级
            maxZoom: 15,
            minZoom: 9
        }),
        target: 'map'
    });

    map.on('click', function (e) {
        // alert('X ; '+ e.clientX  + 'Y: '+e.clientY);
        let t = ol.proj.transform(map.getEventCoordinate(event), 'EPSG:3857', 'EPSG:4326');
        alert("鼠标点击处的经纬度：" + t);
    });

    //给8位字符串文件名补0
    function zeroFill(num, len, radix) {
        var str = num.toString(radix || 10);
        while (str.length < len) {
            str = "0" + str;
        }
        return str;
    }

    // ol.source.XYZ添加瓦片地图的层
    let tileLayer = new ol.layer.Tile({
        source: new ol.source.XYZ({
            tileUrlFunction: function (coordinate) {
                //alert(coordinate[0] + " X= " + coordinate[1] + " Y= " + coordinate[2]);
                var x = 'C' + zeroFill(coordinate[1], 8, 16);
                var y = 'R' + zeroFill(-coordinate[2] - 1, 8, 16);
                var z = 'L' + zeroFill(coordinate[0], 2, 10);
                return '../../../static/map/' + z + '_' + y + '_' + x + '.png';//这里可以修改地图路径
            },
            projection: 'EPSG:3857'
        })
    });
    map.addLayer(tileLayer);//添加到map里面
</script>
</body>
</html>
